import { Component } from "react"

class Son extends Component{
    render(){
        return(
            <div>
                {/* 接收父组件数据 */}
                Son,{this.props.msg}
                {/* 向父组件传递数据 */}
                <button onClick={()=>this.props.getSonMsg('data from son to parent')}> send to parent</button>
            </div>
        )
    }
}

class Parent extends Component{
    state={
        msg:'msg from parent to son',
        data:''
    }
    getSonMsg=(data)=>{
        // 不用担心msg丢失，setState是合并
        this.setState({
            data
        })
    }
    render(){
        return(
            <div>
                {/* 使用子组件数据 */}
                Parent,{this.state.data}
                {/* 向子组件传递数据 */}
                <Son msg={this.state.msg} getSonMsg={this.getSonMsg}></Son>
            </div>
        )
    }
}

const ClassCommunication=()=>{
    return(
        <Parent></Parent>
    )
}
export default ClassCommunication